import { _decorator, CCString, Component, Label, Node, RichText, tween, UIOpacity, Vec3, } from 'cc'
import { Logger } from 'db://assets/scripts/utils/log'
import { tweenConfig } from 'db://assets/scripts/enum/global'
const { ccclass, property } = _decorator

@ccclass('MajorContent')
export class MajorContent extends Component {
    @property({
      tooltip: '对话内容',
      displayName: '对话内容',
      type: RichText,
    })
    majorContent: RichText

    @property({
      tooltip: '头像',
      displayName: '头像',
      type: Node,
    })
    avatar: Node

    @property({
      tooltip: '昵称',
      displayName: '昵称',
      type: Node
    })
    nickName: Node

    @property({
      tooltip: '预设名称',
      displayName: '预设名称',
    })
    namePlacehold: string = '我'

    @property({
      tooltip: '预设内容',
      displayName: '预设内容',
    })
    context: string = ''

    #log = new Logger(`主角弹窗[${this.name}]`)

    protected onLoad() {
      this.#log.debug('*主角对话框加载完毕*')
      this.node.active = false
      this.nickName.getComponent(Label).string = '我'
    }

    protected onDestroy(): void {
        this.#log.debug('*主角对话框销毁*')
    }

    protected onEnable(): void {
        this.#log.debug('*主角对话框显示*')
        this.showDialog(() => {
          this.refreshContext()
        })
    }

    protected onDisable(): void {
        this.#log.debug('*主角对话框隐藏*')
        this.hideDialog()
    }

    set setNickName(str: string) {
      this.nickName.getComponent(Label).string = str
    }

    refreshContext = (cb = () => {}) => {
      this.majorContent.string = ''
      this.schedule(() => {
          this.majorContent.string += this.context[this.majorContent.string.length]
          if(this.majorContent.string == this.context) cb()
      }, 0.02, this.context.length - 1)
    }

    showDialog(call = () => {}){
        this.#log.debug('::showDialog() 展示弹窗', this.node)
        const t2 = tween(this.node.getComponent(UIOpacity)).to(tweenConfig.params.duration.DURATION_2, { opacity: 255 })
        tween(this.node).parallel(t2).call(call).start()
    }

    hideDialog(call = () => {}) {
        this.#log.debug('::hideDialog() 隐藏弹窗')
        const t2 = tween(this.node.getComponent(UIOpacity)).to(tweenConfig.params.duration.DURATION_2, { opacity: 0 })
        tween(this.node).parallel(t2).call(call).start()
    }
}
